"""
审计日志模型
"""
from datetime import datetime
from sqlalchemy import Column, DateTime, Integer, String, Text, JSON, ForeignKey
from sqlalchemy.orm import relationship

from app.core.database import Base


class AuditLog(Base):
    """审计日志模型"""
    __tablename__ = "audit_logs"

    id = Column(Integer, primary_key=True, index=True)
    action = Column(String(100), nullable=False)  # create, update, delete, deploy, rollback
    resource_type = Column(String(50), nullable=False)  # user, application, deployment
    resource_id = Column(Integer)
    resource_name = Column(String(200))
    old_values = Column(JSON)  # Old values before change
    new_values = Column(JSON)  # New values after change
    ip_address = Column(String(45))  # IPv4 or IPv6
    user_agent = Column(String(500))
    created_at = Column(DateTime, default=datetime.utcnow)
    
    # 外键
    user_id = Column(Integer, ForeignKey("users.id"), nullable=False)
    
    # 关联关系
    user = relationship("User", back_populates="audit_logs")
